package com.jwmp.rbac.web.servlet;

import com.jwmp.core.servlet.BaseServlet;
import com.jwmp.core.util.PageResult;
import com.jwmp.core.util.QueryObject;
import com.jwmp.core.util.QueryObjectUtil;
import com.jwmp.rbac.domain.Module;
import com.jwmp.rbac.domain.Permission;
import com.jwmp.rbac.domain.Role;
import com.jwmp.rbac.service.IModuleService;
import com.jwmp.rbac.service.IPermissionService;
import com.jwmp.rbac.service.IRoleService;
import com.jwmp.rbac.service.impl.ModuleServiceImpl;
import com.jwmp.rbac.service.impl.PermissionServiceImpl;
import com.jwmp.rbac.service.impl.RoleServiceImpl;
import com.jwmp.rbac.util.SetPermission;
import com.jwmp.core.util.StringUtil;

import javax.management.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet("/permission/*")
public class PermissionServlet extends BaseServlet {
    private IPermissionService permissionService = new PermissionServiceImpl();
    private IModuleService moduleService = new ModuleServiceImpl();
    private IRoleService roleService = new RoleServiceImpl();

    @SetPermission(moduleName = "权限管理",name="权限列表",expression = "/permission/list")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QueryObject qo = QueryObjectUtil.req2qo(req);

        PageResult pageResult = permissionService.queryList(qo);
        req.setAttribute("pageResult",pageResult);
        req.getRequestDispatcher("/WEB-INF/views/rbac/permission_list.jsp").forward(req,resp);
    }

    // 权限编辑页面
    @SetPermission(moduleName = "角色管理",name="角色授权页",expression = "/permission/permissionEdit")
    public void permissionEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String strRoleId = req.getParameter("roleId");
        if (StringUtil.hasLength(strRoleId)) {
            Long roleId = Long.valueOf(strRoleId);
            // 查询当前角色拥有的权限列表
            Role role = roleService.selectOne(roleId);
            // 查询当前角色没有的模块列表
            List<Module> leftModuleList = moduleService.selectRoleNoPermissionModules(roleId);

            req.setAttribute("role",role);
            req.setAttribute("leftModuleList",leftModuleList);
        }

        req.getRequestDispatcher("/WEB-INF/views/rbac/role-permission.jsp").forward(req,resp);
    }

    // 给角色授权
    @SetPermission(moduleName = "角色管理",name="角色授权",expression = "/permission/authorizeForRole")
    public void authorizeForRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        String[] pIds = req.getParameterValues("pId");

        if (StringUtil.hasLength(roleId) && pIds != null) {

            List<Long> pIdList = Arrays.stream(pIds)
                    .map(sPid -> StringUtil.hasLength(sPid) ? Long.valueOf(sPid) : 0L)
                    .filter(id -> id != 0)
                    .collect(Collectors.toList());

            permissionService.authorizeForRole(Long.valueOf(roleId),pIdList);
        }
        permissionEdit(req,resp);
    }

    // 加载权限
    @SetPermission(moduleName = "权限管理",name="重新加载权限",expression = "/permission/reload")
    public void reload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        permissionService.loadPremissionToDB("com.jwmp.rbac.web.servlet");
        resp.getWriter().print("success");
    }

}
